<script>
    $(document).ready(function () {
        //添加
        $("#add-sub").click(function () {
            var prj = $("#prj-name").val();
            $.ajax({
                type:"POST",
                async:false,
                url:"/item/addprj/",
                data:{"prj_name":prj},
                dataType : "json",
                success:function (data) {
                    $("#addModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //获取修改内容
        $("td>a[name='edit-prj']").click(function () {
            var id = $(this).attr("prj_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/item/idprj/",
                data:{"id":id },
                dataType: "json",
                success:function (data) {
                    $("#edit-prj-name").val(data.PrjName);
                    $("#edit-sub").attr("prj_id",data.ID);
                    $("#editModal").modal("show");
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });

        //提交修改内容
        $("#edit-sub").click(function () {
            var id = $(this).attr("prj_id");
            var prj_name = $("#edit-prj-name").val();
            $.ajax({
                type:"PUT",
                async:false,
                url:"/item/editprj/",
                data:{"id":id,"prj_name":prj_name},
                dataType : "json",
                success:function (data) {
                    $("#editModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //删除
        $("td>a[name='del-prj']").click(function () {
            var id = $(this).attr('prj_id');
            var statu = confirm("是否确认删除!");
            if (statu==true)
            {
                $.ajax({
                    type: "post",
                    async:false,
                    url: "/item/delprj/",
                    data: {"id":id},
                    dataType : "json",
                    success: function(data) {
                        spop({
                            template:data,
                            style: 'warning',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()",2100);
                    },
                    error:function (data) {
                        spop({
                            template:data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //搜索角色
        $("#search-key").bind('change',function () {
            var key = $(this).val();
            if (key != "" ) {
                var url = "/item/searchprj/" + key + "/";

            }else{
                var url = "/item/prj/";
            }
            $("#search-a").attr("href", url);
        });

    })
</script>
